import Player from "./player.js";
import InputHandler from './input.js';
import { Background} from "./background.js";
import {FlyingEnemy,GroundEnemy,ClimbingEnemy} from './enemy.js'
import { UI } from "./UI.js";


window.addEventListener('load',function(){
    const canvas=document.querySelector('#canvas1');
    const ctx=canvas.getContext('2d');
    canvas.width=500;
    canvas.height=500;

    class Game{
        constructor(width,height){
            this.width=width;
            this.height=height;
            this.groundMargin=80
            this.speed=0;
            this.maxSpeed=5;
            this.background=new Background(this)
            this.player=new Player(this)
            this.input=new InputHandler(this);
            this.particles=[];
            this.maxparticle=50;
            this.collitions=[];
            this.enemies=[];
            this.addEnemy()
            this.enemyTimer=0;
            this.enemyInterval=1000;
            this.score=0;
            this.timer=0;
            this.maxTime=30000;
            this.UI=new UI(this)
            this.heart=5;
            this.gameOver=false;
        }

        update(deltaTime){
            //heart为0时间结束
            if(this.heart<=0)
                this.gameOver=true;

            //时间截止游戏结束
            if(this.timer>this.maxTime)
                this.gameOver=true;
            else this.timer+=deltaTime;

            this.player.update(this.input.keys,deltaTime)
            this.background.update()

            //生成enemy
            if(this.enemyTimer>this.enemyInterval){
                this.addEnemy();
                this.enemyTimer=0
            }else{
                this.enemyTimer+=deltaTime;
            }
            this.enemies.forEach(enemy=>{
                enemy.update(deltaTime)
            })

            //粒子效果更新
            if(this.particles.length>this.maxparticle){
                this.particles=this.particles.slice(0,this.maxparticle)
            }
            this.particles.forEach((particle)=>{
                particle.update();
            })

            //爆炸动画更新
            this.collitions.forEach((collition)=>{
                collition.update(deltaTime)
            })

            //清除deleteFlag为true的元素
            this.enemies=this.enemies.filter(enemy=>!enemy.deleteFlag)
            this.particles=this.particles.filter(particle=>!particle.deleteFlag);
            this.collitions=this.collitions.filter(collition=>!collition.deleteFlag)
        }

        draw(){
            this.background.draw(ctx)
            this.player.draw(ctx)
            this.enemies.forEach(enemy=>{
                enemy.draw(ctx)
            })
            this.particles.forEach(particle=>{
                particle.draw(ctx);
            })
            this.collitions.forEach(collition=>{
                collition.draw(ctx);
            })
            this.UI.draw(ctx)
            
        }
        addEnemy(){
            if(this.speed>0&& Math.random()>0.5)
                this.enemies.push(new GroundEnemy(this))
            else if(this.speed>0) this.enemies.push(new ClimbingEnemy(this))
            this.enemies.push(new FlyingEnemy(this))
        }
        start(){
            this.score=0;
            this.timer=0;
            this.gameOver=false;
            this.heart=5;
            this.enemies=[];
            this.particles=[];
            this.collitions=[];
            this.player.setState(0,0);
            this.player.x=0;
            this.player.y = this.height - this.player.height - this.groundMargin;
            animate(0)
        }
    }
    const game=new Game(canvas.width,canvas.height);

    let lastTime=0;
    function animate(timestamp){
        ctx.clearRect(0,0,canvas.width,canvas.height)
        let deltaTime=timestamp-lastTime;
        lastTime=timestamp;
        game.update(deltaTime);
        game.draw();

        if(!game.gameOver) requestAnimationFrame(animate)
    }
    animate(0)
})